//package com.common.aop;
//
//
//import com.common.annotation.ApiLimit;
//import com.common.cache.RedisClientTemplate;
//import com.common.exception.SubmitException;
//import com.common.util.SecreUtil;
//import org.apache.log4j.Logger;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.lang.annotation.Annotation;
//import java.lang.reflect.Method;
//import java.util.Arrays;
//import java.util.concurrent.TimeUnit;
//
///**
// * Created by dell on 2016/8/5.
// */
//@Aspect
//@Component
//public class ApiLimitAspect {
//
//    private static Logger logger = Logger.getLogger(ApiLimitAspect.class);
//
//    @Autowired
//    private RedisClientTemplate redisClientTemplate;
//
//    @Pointcut("@annotation(com.common.annotation.ApiLimit)")
//    public  void apiLimit() {
//
//    }
//
//    @Around("apiLimit()")
//    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//        limitNum(joinPoint);
//        limitCommit(joinPoint);
//        Object result = joinPoint.proceed(joinPoint.getArgs());
//        return result;
//    }
//
//
//    private void limitCommit(ProceedingJoinPoint joinPoint){
//
//        ApiLimit apiLimit=getAnnotation(joinPoint,ApiLimit.class);
//        if(apiLimit.repeatCommit()==true){
//            String mKey=getKey(joinPoint);
//            Object token= SecreUtil.getSessionValue("form_token");
//            if(token==null){
//                SecreUtil.putSessionValue("form_token",mKey);
//            }else {
//                if(mKey.equals(token)){
//                    throw new SubmitException(SubmitException.Code.SUBMITREPEAT,"系统不允许你重复提交数据");
//                }else {
//                    SecreUtil.putSessionValue("form_token",mKey);
//                }
//            }
//        }
//    }
//
//    /**
//     * 接口访问频率限制
//     * @param joinPoint
//     */
//    private void limitNum(ProceedingJoinPoint joinPoint){
//        ApiLimit apiLimit=getAnnotation(joinPoint,ApiLimit.class);
//        if(apiLimit.hzNum()>0){
//            String mKey=getKey(joinPoint);
//            Object value=redisClientTemplate.get(mKey);
//            if(value==null){
//                redisClientTemplate.set(mKey,apiLimit.hzNum());
//                redisClientTemplate.expire(mKey,apiLimit.hzNum(), TimeUnit.MILLISECONDS);
//            }else {
//                throw new SubmitException(SubmitException.Code.HZNUM,"接口访问限制,你访问太频繁");
//            }
//        }
//    }
//
//    /**
//     * 根据类名、方法名和参数值获取唯一的缓存键
//     * @return 格式为 "包名.类名.方法名.参数类型.参数值"，类似 "your.package.SomeService.getById(int).123"
//     */
//    private String getKey(ProceedingJoinPoint joinPoint) {
//        Object[] params=joinPoint.getArgs();
//        int kye=0;
//        kye= Arrays.hashCode(params);
//        String key= String.format("%s.%s",
//                joinPoint.getSignature().toString().split("\\s")[1], kye);
//        return "api-method-"+SecreUtil.getIp()+"-"+key;
//    }
//
//    private <T extends Annotation> T getAnnotation(ProceedingJoinPoint jp, Class<T> clazz) {
//        MethodSignature sign = (MethodSignature) jp.getSignature();
//        Method method = sign.getMethod();
//        return method.getAnnotation(clazz);
//    }
//}
